<template>
  <div>
    <home-header></home-header>
    <home-swiper :swiperList="swiperList"></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>
<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import {mapState} from 'vuex'
export default {
  name: 'Home',
  data(){
    return{
      lastCity:'', //上一次页面显示的城市
      swiperList:[],
      iconList:[],
      recommendList:[],
      weekendList:[],
    }
  },
  components: { HomeHeader,HomeSwiper,HomeIcons,HomeRecommend,HomeWeekend},
  computed:{
    ...mapState(['city'])
  },
  methods:{
    getHomeInfo(){
      axios.get('./api/index.json?city='+this.city).then((res)=>{
        // 记得加判断
        // console.log(this.city)
        // console.log(res.data[this.city],999)
        if(res.data.ret && res.data.data){
          this.swiperList=res.data.data.swiperList
          this.iconList=res.data.data.iconList
          this.recommendList=res.data.data.recommendList
          this.weekendList=res.data.data.weekendList
        }
      })
     
    }
  },
  mounted(){
    // 页面加载的时候保存上一次显示的城市
    this.lastCity=this.city
    // 请求的方法写在methods里面
    this.getHomeInfo()
  },
  // 当使用keep-alive，会多一个生命周期函数
  // 当页面重新显示的时候执行activated
  // 可以改变缓存的数据
  activated(){
    // 如果选择的城市不是上一次的城市，重新发ajax请求
    // 把这次的城市保存
    if(this.lastCity!==this.city){
      this.getHomeInfo()
      this.lastCity=this.city
    }
    //  axios.get('./api/index.json?id=0001').then((res)=>{
    //     console.log(this.$router)
    //   })
  }

}
</script>
<style scoped>
</style>
